package com.sduept.nwld.dataserver.manager.countanalyse;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sduept.nwld.dataserver.manager.statistics.FaultTimeAnalysisCountManager;
import com.sduept.nwld.dataserver.manager.statistics.FaultTimeAnalysisManager;
import com.sduept.nwld.dataserver.manager.statistics.ProtectKpiCountManager;
import com.sduept.nwld.dataserver.manager.statistics.ProtectKpiManager;
import com.sduept.nwld.dataserver.manager.statistics.StatisticsCommonManager;

/**
 * 定时服务管理，统计分析，需要定时进行统计
 * 重构为 StatisticalAnalysisSchedule 
 */
@Service
@Deprecated
public class StatisticsCountTimerService {
	@Autowired
	private ProtectKpiManager manager;
	@Autowired
	private StatisticsCommonManager commonM;
	@Autowired
	private ProtectKpiCountManager countM;
	@Autowired
	private FaultTimeAnalysisManager faultTimeM;
	@Autowired
	private FaultTimeAnalysisCountManager faultTimeCountM;

	/**
	 * 每月第一天的零点，自动统计保护正确动作率和故障快速切除率
	 */
//	@Schedule(second = "0", minute = "0", hour = "0", dayOfMonth = "1", persistent = false)
//	@Schedule(second = "0", minute = "*/1", hour = "*", persistent = false) // 每隔一分钟执行一次，测试用，看是否走定时器
//	@Scheduled(cron = "0 0 0 1 * ?")
	public void statisticalDataTimely () {
		Integer[] date = getPreviousMonth();
		Integer year = date[0];
		Integer month = date[1];
		List<Integer> voltages = commonM.getVoltages();
		// 统计保护动作正确率
//		countProtectCorrectAct(year, month, voltages);
		countProtectCorrectActTwo(year, month, voltages);
		// 统计故障快速切除率
//		countFaultFastRemove(year, month, voltages);
		countFaultFastRemoveTwo(year, month, voltages);
		// 统计故障持续时间
		countFaultTime(year, month, voltages);
		// 统计保护动作时间
		countProtectActTime(year, month, voltages);
		// 统计断路器动作时间
		countBreakerActTime(year, month, voltages);
	}
	
	/**
	 * 统计保护动作正确率
	 * @param year
	 * @param month
	 */
	private void countProtectCorrectAct(Integer year,Integer month,List<Integer> voltages) {
		manager.deleteProtectCorrectAct(year, month);
		for (Integer vol : voltages) {
			countM.countProtectCorrectAct(year, month,vol);
		}
	}
	
	/**
	 * 统计保护正确动作率
	 * @param year
	 * @param month
	 * @param voltages
	 */
	private void countProtectCorrectActTwo(Integer year, Integer month, List<Integer> voltages) {
		manager.deleteProtectCorrectAct(year, month, 0, true);
		manager.deleteProtectCorrectAct(year, month, 1, true);
		for(Integer vol : voltages) {
			countM.countProtectCorrectAct(year, month, vol, 0, true);
			countM.countProtectCorrectAct(year, month, vol, 1, true);
		}
	}
	
	/**
	 *统计故障快速切除率
	 * @param year
	 * @param month
	 */
	private void countFaultFastRemove(Integer year,Integer month,List<Integer> voltages) {
		manager.deleteFaultFastRemove(year, month);
		for (Integer vol : voltages) {
			countM.countFaultFastRemove(year, month,vol);
		}
	}
	
	private void countFaultFastRemoveTwo(Integer year, Integer month, List<Integer> voltages) {
		manager.deleteFaultFastRemove(year, month, 0, true);
		manager.deleteFaultFastRemove(year, month, 1, true);
		for(Integer vol : voltages) {
			countM.countFaultFastRemove(year, month, vol, 0, true);
			countM.countFaultFastRemove(year, month, vol, 1, true);
		}
	}
	
	/**
	 * 统计故障持续时间
	 * @param year
	 * @param month
	 */
	private void countFaultTime(Integer year,Integer month,List<Integer> voltages) {
		faultTimeM.deleteFaultCount(year, month);
		for (Integer vol : voltages) {
			faultTimeCountM.countFaultContinuedTime(year, month, vol);
		}
	}
	/**
	 * 统计保护动作时间
	 * @param year
	 * @param month
	 */
	private void countProtectActTime(Integer year,Integer month,List<Integer> voltages) {
		faultTimeM.deleteProtectActTimeCount(year, month);
		for (Integer vol : voltages) {
			faultTimeCountM.countProtectActTimeCount(year,month,vol);
		}
	}
	
	/**
	 * 统计断路器动作时间
	 * @param year
	 * @param month
	 */
	private void countBreakerActTime(Integer year,Integer month,List<Integer> voltages) {
		faultTimeM.deleteBreakerActTimeCount(year, month);
		for (Integer vol : voltages) {
			faultTimeCountM.countBreakerActTimeCount(year,month,vol);
		}
	}
	/**
	 * 获取当前时间的上一个月
	 * @return
	 */
	private Integer[] getPreviousMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1); //当前月的上个月
        String ym = sdf.format(cal.getTime());
        String[] dt = ym.split("-");
		Integer[] dts = {Integer.parseInt(dt[0]),Integer.parseInt(dt[1])};
		return dts;
	}
}
